var mongoose = require('mongoose');
var Schema = mongoose.Schema;

mongoose.connect("mongodb://localhost:27020/makeblock-iot");

var ui_pub_sub = new Schema({
	topic: String,
	data: String,
	timestamp: Number
}/*,{ minimize: false }*/);

ui_pub_sub.methods.QueryDataLimit = function(userid, limit, callback) {
	return this.model('ui_pub_sub_collections').find(userid, callback).select('data timestamp').sort({'timestamp': -1}).limit(limit);
}

ui_pub_sub.methods.QueryDataBetweenTimeLimit = function(userid, begintime, endtime, limit, callback) {
	return this.model('ui_pub_sub_collections').find(userid, callback).where('timestamp').gte(begintime).lt(endtime).select('data timestamp').sort({'timestamp': -1}).limit(limit);
}

exports.ui_pub_sub=mongoose.model('ui_pub_sub_collections', ui_pub_sub);
